<?php

namespace App\Http\Controllers;

use App\Test;
use ElemeOpenApi\Api\ProductService;
use ElemeOpenApi\Config\Config;
use Illuminate\Http\Request;

class CategoryController extends Controller
{


    
    /** 查询店铺商品分类
     * @param $shop_id 店铺Id
     * @return mixed
     */
    public function getShopCategories(Request $request)
    {
        $requestData = $request->only([
            'shop_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });  
                $service = new ProductService(json_decode($token),$config);
                $dates = $service->get_shop_categories($requestData['shop_id']);
                foreach($dates as $date){
                    $data[] = [
                        'product_id' => $date->id,
                        'product_name' => $date->name,
                        'product_description' => $date->description,
                        'product_isValid' => $date->isValid,
                        'product_parentId' => $date->parentId,
                        'product_children' => $date->children
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }


    /** 查询店铺商品分类，包含二级分类
     * @param $shop_id 店铺Id
     * @return mixed
     */
    public function getShopCategoriesWithChildren(Request $request)
    {
        $requestData = $request->only([
            'shop_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });  
                $service = new ProductService(json_decode($token),$config);
                $dates = $service->get_shop_categories_with_children($requestData['shop_id']);
                foreach($dates as $date){
                    $data[] = [
                        'product_id' => $date->id,
                        'product_name' => $date->name,
                        'product_description' => $date->description,
                        'product_isValid' => $date->isValid,
                        'product_parentId' => $date->parentId,
                        'product_children' => $date->children
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }


    /** 查询商品分类详情
     * @param $category_id 商品分类Id
     * @return mixed
     */
    public function getCategory(Request $request)
    {
        $requestData = $request->only([
            'category_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });  
                $service = new ProductService(json_decode($token),$config);
                $dates = $service->get_category($requestData['category_id']);
                foreach($dates as $date){
                    $data[] = [
                        'product_id' => $date->id,
                        'product_name' => $date->name,
                        'product_description' => $date->description,
                        'product_isValid' => $date->isValid,
                        'product_parentId' => $date->parentId,
                        'product_children' => $date->children
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 查询商品分类详情，包含二级分类
     * @param $category_id 商品分类Id
     * @return mixed
     */
    public function getCategoryWithChildren(Request $request)
    {
        $requestData = $request->only([
            'category_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });  
                $service = new ProductService(json_decode($token),$config);
                $dates = $service->get_category_with_children($requestData['category_id']);
                dd($dates);
                foreach($dates as $date){
                    $data[] = [
                        'product_id' => $date->id,
                        'product_name' => $date->name,
                        'product_description' => $date->description,
                        'product_isValid' => $date->isValid,
                        'product_parentId' => $date->parentId,
                        'product_children' => $date->children
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }





    /** 查询商品后台类目
     * @param $shop_id 店铺Id
     * @return mixed
     */
    public function getBackCategory(Request $request)
    {
        $requestData = $request->only([
            'shop_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });  
                $service = new ProductService(json_decode($token),$config);
                $dates = $service->get_back_category($requestData['shop_id']);
                foreach($dates as $date){
                    $data[] = [
                        'category_id' => $date->id,
                        'category_name' => $date->name,
                        'category_parentId' => $date->parentId,
                        'category_lev' => $date->lev,
                        'category_children' => $date->children,
                        'category_leaf' => $date->leaf
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 获取一个分类下的所有商品
     * @param $category_id 商品分类Id
     * @return mixed
     */
    public function getItemsByCategoryId(Request $request)
    {
        $requestData = $request->only([
            'category_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });  
                $service = new ProductService(json_decode($token),$config);
                $dates = $service->get_items_by_category_id($requestData['category_id']);
                foreach($dates as $date){
                    $data[] = [
                        'commodity_description' => $date->description,
                        'commodity_id' => $date->id,
                        'commodity_name' => $date->name,
                        'commodity_isValid' => $date->isValid,
                        'commodity_recentPopularity' => $date->recentPopularity,
                        'commodity_categoryId' => $date->categoryId,
                        'commodity_shopId' => $date->shopId,
                        'commodity_shopName' => $date->shopName,
                        'commodity_imageUrl' => $date->imageUrl,
                        'commodity_labels' => $date->labels,
                        'commodity_specs' => $date->specs,
                        'commodity_sellingTime' => $date->sellingTime,
                        'commodity_attributes' => $date->attributes,
                        'commodity_backCategoryId' => $date->backCategoryId,
                        'commodity_minPurchaseQuantity' => $date->minPurchaseQuantity,
                        'commodity_unit' => $date->unit,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }

    }

    /** 查询商品详情
     * @param $item_id 商品Id
     * @return mixed
     */
    public function getItem(Request $request)
    {
        $requestData = $request->only([
            'item_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });  
                $service = new ProductService(json_decode($token),$config);
                $dates = $service->get_item($requestData['item_id']);
                dd($dates);
                foreach($dates as $date){
                    $data[] = [
                        'commodity_description' => $date->description,
                        'commodity_id' => $date->id,
                        'commodity_name' => $date->name,
                        'commodity_isValid' => $date->isValid,
                        'commodity_recentPopularity' => $date->recentPopularity,
                        'commodity_categoryId' => $date->categoryId,
                        'commodity_shopId' => $date->shopId,
                        'commodity_shopName' => $date->shopName,
                        'commodity_imageUrl' => $date->imageUrl,
                        'commodity_labels' => $date->labels,
                        'commodity_specs' => $date->specs,
                        'commodity_sellingTime' => $date->sellingTime,
                        'commodity_attributes' => $date->attributes,
                        'commodity_backCategoryId' => $date->backCategoryId,
                        'commodity_minPurchaseQuantity' => $date->minPurchaseQuantity,
                        'commodity_unit' => $date->unit,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 批量查询商品详情
     * @param $item_ids 商品Id的列表
     * @return mixed
     */
    public function batchGetItems(Request $request)
    {
        $requestData = $request->only([
            'item_ids','type','unique_id'
        ]);
        $item_ids =$requestData['item_ids'];
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });  
                $service = new ProductService(json_decode($token),$config);
                $dates = $service->batch_get_items(json_decode($item_ids));
                foreach($dates as $date){
                    $data[] = [
                        'commodity_description' => $date->description,
                        'commodity_id' => $date->id,
                        'commodity_name' => $date->name,
                        'commodity_isValid' => $date->isValid,
                        'commodity_recentPopularity' => $date->recentPopularity,
                        'commodity_categoryId' => $date->categoryId,
                        'commodity_shopId' => $date->shopId,
                        'commodity_shopName' => $date->shopName,
                        'commodity_imageUrl' => $date->imageUrl,
                        'commodity_labels' => $date->labels,
                        'commodity_specs' => $date->specs,
                        'commodity_sellingTime' => $date->sellingTime,
                        'commodity_attributes' => $date->attributes,
                        'commodity_backCategoryId' => $date->backCategoryId,
                        'commodity_minPurchaseQuantity' => $date->minPurchaseQuantity,
                        'commodity_unit' => $date->unit,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }




    /** 根据商品扩展码获取商品                        这个系统给的扩展码没给，所以没办法测试,没办法重新命名
     * @param $shop_id 店铺Id
     * @param $extend_code 商品扩展码
     * @return mixed
     */
    public function getItemByShopIdAndExtendCode(Request $request)
    {
        $requestData = $request->only([
            'shop_id','extend_code','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });  
                $service = new ProductService(json_decode($token),$config);
                $dates = $service->get_item_by_shop_id_and_extend_code($requestData['shop_id'],$requestData['extend_code']);
                dd($dates);
                foreach($dates as $date){
                    $data[] = [
                        'commodity_description' => $date->description,
                        'commodity_id' => $date->id,
                        'commodity_name' => $date->name,
                        'commodity_isValid' => $date->isValid,
                        'commodity_recentPopularity' => $date->recentPopularity,
                        'commodity_categoryId' => $date->categoryId,
                        'commodity_shopId' => $date->shopId,
                        'commodity_shopName' => $date->shopName,
                        'commodity_imageUrl' => $date->imageUrl,
                        'commodity_labels' => $date->labels,
                        'commodity_specs' => $date->specs,
                        'commodity_sellingTime' => $date->sellingTime,
                        'commodity_attributes' => $date->attributes,
                        'commodity_backCategoryId' => $date->backCategoryId,
                        'commodity_minPurchaseQuantity' => $date->minPurchaseQuantity,
                        'commodity_unit' => $date->unit,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 根据商品条形码获取商品          这个系统给的扩展码没给，所以没办法测试,没办法重新命名
     * @param $shop_id 店铺Id
     * @param $bar_code 商品条形码
     * @return mixed
     */
    public function getItemsByShopIdAndBarCode(Request $request)
    {
        $requestData = $request->only([
            'shop_id','bar_code','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });  
                $service = new ProductService(json_decode($token),$config);
                $dates = $service->get_items_by_shop_id_and_bar_code($requestData['shop_id'],$requestData['bar_code']);
                foreach($dates as $date){
                    $data[] = [
                        'commodity_description' => $date->description,
                        'commodity_id' => $date->id,
                        'commodity_name' => $date->name,
                        'commodity_isValid' => $date->isValid,
                        'commodity_recentPopularity' => $date->recentPopularity,
                        'commodity_categoryId' => $date->categoryId,
                        'commodity_shopId' => $date->shopId,
                        'commodity_shopName' => $date->shopName,
                        'commodity_imageUrl' => $date->imageUrl,
                        'commodity_labels' => $date->labels,
                        'commodity_specs' => $date->specs,
                        'commodity_sellingTime' => $date->sellingTime,
                        'commodity_attributes' => $date->attributes,
                        'commodity_backCategoryId' => $date->backCategoryId,
                        'commodity_minPurchaseQuantity' => $date->minPurchaseQuantity,
                        'commodity_unit' => $date->unit,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }




    /** 查询活动商品  为空，也没改商品命名
     * @param $shop_id 店铺Id
     * @return mixed
     */
    public function getItemIdsHasActivityByShopId(Request $request)
    {
        $requestData = $request->only([
            'shop_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 });  
                $service = new ProductService(json_decode($token),$config);
                $dates = $service->get_item_ids_has_activity_by_shop_id($requestData['shop_id']);
                dd($dates);
                foreach($dates as $date){
                    $data[] = [
                        'commodity_description' => $date->description,
                        'commodity_id' => $date->id,
                        'commodity_name' => $date->name,
                        'commodity_isValid' => $date->isValid,
                        'commodity_recentPopularity' => $date->recentPopularity,
                        'commodity_categoryId' => $date->categoryId,
                        'commodity_shopId' => $date->shopId,
                        'commodity_shopName' => $date->shopName,
                        'commodity_imageUrl' => $date->imageUrl,
                        'commodity_labels' => $date->labels,
                        'commodity_specs' => $date->specs,
                        'commodity_sellingTime' => $date->sellingTime,
                        'commodity_attributes' => $date->attributes,
                        'commodity_backCategoryId' => $date->backCategoryId,
                        'commodity_minPurchaseQuantity' => $date->minPurchaseQuantity,
                        'commodity_unit' => $date->unit,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }
}
